﻿using BLL;
using EntitiesLayer;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using WcfServices.Utilities;

namespace WcfServices
{
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ParametroService 
    {
        private GestorParametrosSistema gestor;

        public ParametroService() {
            gestor = new GestorParametrosSistema();
        }

        [OperationContract]
        [WebInvoke(Method = "GET",
            BodyStyle = WebMessageBodyStyle.Bare,
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json)]        
        public string obtenerParametros()
        {
            ParametrosSistema parametros =
            Util.interceptWithReturn(gestor.ObtenerParametrosSistema);

            if (parametros != null)
                return parametros.JsonSerialize(ignoreTypes: new[] { typeof(IList<>) });

            return "";
        }


        [OperationContract]
        [WebInvoke(Method = "PUT",
            BodyStyle = WebMessageBodyStyle.Bare,
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json)]
        public void actualizarParametros(Stream json)
        {
            IDictionary<string, string> data = json.DeserializeJson<Dictionary<string, string>>();

            gestor.ModificarParametrosSistema(
                data["NombreGimnasio"], data["RazonSocialGimnasio"], Convert.ToInt32(data["frecuenciaRecordatorioMedida"]),
                Convert.ToDecimal(data["CostoMembresia"]), Convert.ToDecimal(data["CostoMatricula"]), Convert.ToInt32(data["frecuenciaCobroMembresia"]),
                data["RemitenteEnvioCorreo"], data["HostEnvioCorreo"], Convert.ToInt32(data["PuertoEnvioCorreo"]),
                data["NombreUsuarioEnvioCorreo"], data["ContrasennaEnvioCorreo"], Convert.ToDecimal(data["ConstanteMujerPorcentajeGrasa"]),
                Convert.ToDecimal(data["ConstanteHombrePorcentajeGrasa"]), Convert.ToDecimal(data["ConstanteSumandoPorcentajeGrasa"])
                );
        }
    }
}
